package com.apollo.demo.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
public class GlobalExceptionHandler {

    private ErrorInfo<String> buildErrorInfo(BaseException e) {
        ErrorInfo<String> r = new ErrorInfo<>();
        r.setMessage(e.getMessage());
        r.setCode(e.getCode());
        r.setData(e.getData());
        return r;
    }

    @ExceptionHandler(ItemNotFoundException.class)
    @ResponseBody
    @ResponseStatus(value = HttpStatus.NOT_FOUND)
    public ErrorInfo<String> handlerItemNotFoundException(ItemNotFoundException e){
        return buildErrorInfo(e);
    }

    @ExceptionHandler(NotImplementedException.class)
    @ResponseBody
    @ResponseStatus(value = HttpStatus.NOT_IMPLEMENTED)
    public ErrorInfo<String> handlerNotImplementedException(NotImplementedException e){
        return buildErrorInfo(e);
    }

}